#!/usr/bin/env python

import sys
from tools import ping, linux_ping
from time import time

INF = 1000

def ej1(host, rep):
  	num_req = rep
  	t_total = 0 
  	
	for x in range(rep):
		t0 = time()
		resp = ping(host)
		t1 = time()
		
		if resp == None:
			num_req -= 1
		else:
			t_total += t1 - t0
		

	if num_req == 0:
		return INF
		
	return '%.3f' %(t_total/num_req * 10**3)

if __name__ == "__main__":
	if len(sys.argv) < 3:
		print "ejercicio1.py [host] [repeat]"
		exit()
	
	host = sys.argv[1]
	rep = int(sys.argv[2])

	res = ej1(host, rep)
	print "ping a ", host, res, "ms"	
